package lumis.portal.structure;

import lumis.portal.page.PageConfig;

/**
 * A Helper for persistence of layout properties.
 *
 * @version $Revision: 11804 $ $Date: 2010-07-29 10:06:21 -0300 (Thu, 29 Jul 2010) $
 * @since 5.6.0
 */
public class LayoutPropertiesHelper
{
	private final ILayoutPropertiesAccessor layoutPropertiesAccessor;

	/**
	 * The constructor for {@link LayoutPropertiesHelper}.
	 * 
	 * @param layoutPropertiesAccessor the object used to access the layout properties.
	 * @since 5.6.0
	 */
	public LayoutPropertiesHelper(ILayoutPropertiesAccessor layoutPropertiesAccessor)
	{
		this.layoutPropertiesAccessor = layoutPropertiesAccessor;
	}
	
	/**
	 * Changes the layout file path, updating other layout properties as appropriated.
	 * <p>
	 * The <code>layoutFile</code> parameter cannot be an empty string or <code>null</code>.
	 * If it is <code>null</code> the attribute is removed and the attribute layoutType is 
	 * automatically changed to {@link PageConfig#PAGE_LAYOUT_TYPE_INHERIT} if the current value 
	 * is {@link PageConfig#PAGE_LAYOUT_TYPE_LAYOUT_FILE}, else if it is a valid 
	 * value the attribute layoutType is automatically changed to {@link PageConfig#PAGE_LAYOUT_TYPE_LAYOUT_FILE}.
	 * 
	 * @param layoutFilePath the path relative to the <code>&lt;lumisdata&gt;/def</code> directory.
	 * @since 5.6.0
	 */
	public void setLayoutFilePath(String layoutFilePath)
	{
		if (layoutFilePath == null)
			throw new IllegalArgumentException("layoutFile cannot be null");
		else if (layoutFilePath.length() == 0)
			throw new IllegalArgumentException("layoutFile cannot be an empty string");
		
		layoutPropertiesAccessor.setLayoutType(PageConfig.PAGE_LAYOUT_TYPE_LAYOUT_FILE);
		layoutPropertiesAccessor.setPropertyValue(PageConfig.LAYOUT_FILE_PATH_PROPERTY_NAME, layoutFilePath);
	}
	
	/**
	 * Sets the layout type for pages.
	 * <p>
	 * The layout type have to be a valid type and may not be set to {@link PageConfig#PAGE_LAYOUT_TYPE_LAYOUT_FILE}.
	 * If want to set to it, use {@link #setLayoutFilePath(String)} and the layout type will be changed automatically.
	 * <p>
	 * This method may not be used to set the layout type to {@link PageConfig#PAGE_LAYOUT_TYPE_LAYOUT_FILE}.
	 * Use the method {@link #setLayoutFilePath(String)} instead, and the layout type is changed automatically.
	 * <p>
	 * If set to a value different of {@link PageConfig#PAGE_LAYOUT_TYPE_LAYOUT_FILE}, the {@value PageConfig#LAYOUT_FILE_PATH_PROPERTY_NAME}
	 * property will be removed.
	 * 
	 * @see #setLayoutFilePath(String)
	 * 
	 * @param layoutType the layout type.
	 * @since 5.6.0
	 */
	public void setLayoutType(int layoutType)
	{
		// layout type should never be set to PageConfig.PAGE_LAYOUT_TYPE_LAYOUT_FILE
		if(PageConfig.PAGE_LAYOUT_TYPE_LAYOUT_FILE == layoutType)
		{
			throw new IllegalArgumentException("Layout type could not be set to " + PageConfig.PAGE_LAYOUT_TYPE_LAYOUT_FILE + ".");
		}
		else
		{
			// when set the layout type clear the layout file
			layoutPropertiesAccessor.setLayoutType(layoutType);
			layoutPropertiesAccessor.removeProperty(PageConfig.LAYOUT_FILE_PATH_PROPERTY_NAME);
		}
	}
}
